from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from admincharge.utils.jwt import MyJWTAuthentication
from utils.pagination import PetMessagePagination
from admincharge.models import FosterPet,PetOwner
from PetNetadmin.serializers import FosterMessageSerializer

class GetNetShopFosterList(APIView):
    def get(self,request):
        username = request.GET.get('username')
        foster_master_all_list = FosterPet.objects.filter(pet_foster_master=username)
        serializer = FosterMessageSerializer(instance=foster_master_all_list, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return Response(result)
    def post(self,request):
        # 新增建档档案
        data = request.data
        data['is_state']=0
        data['is_checkout']=2
        print('data',data)
        serializer = FosterMessageSerializer(data=data, partial=True)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            result = {
                'code': 2000,
                'msg': '预约成功'
            }
            return Response(status=status.HTTP_200_OK, data=result)
        return Response({"msg": '出了点问题噢!!!'}, status=status.HTTP_400_BAD_REQUEST)

# 已取消预约
@api_view(['POST'])
def deleteOneFostermsg(request, id):
    try:
        FosterMsg = FosterPet.objects.get(pk=id)
        FosterMsg.delete()
        result = {
            'code': 2000,
            'msg': '预约已取消'
        }
        return Response(data=result, status=status.HTTP_200_OK)
    except FosterPet.DoesNotExist:
        return Response({"msg": '当前没有该记录可以删除'}, status=status.HTTP_404_NOT_FOUND)